This topic tells you how to specify and manipulate numeric data in scripts.
Function | Description and Example |
---|---|
Number(str) |
Takes an input string, converts the string to a number, and returns the number. The following example returns the number 12.12. Number("12.12"); |
isNaN(str) |
Returns TRUE if the string is not a number. The following example returns the boolean value TRUE. isNaN(Number("abc")); |
num=parseNumber(str) |
Parses a number from a string and returns the number. The following example returns the number 12.11. parseNumber('$12.11'); |
str=numberFormat(num, pattern) |
Formats the input number using the input number format pattern. The following example returns the string $123.46. numberFormat(123.456789,"$#.00"); |
Function | Description and Example |
---|---|
Math.abs(num) |
Returns the absolute value of num. The following example returns 47. Math.abs(-47); |
Math.min(num1,num2,...) |
Returns the smallest input number. The following example returns -2. Math.min(-2,-1,0,1,2); |
Math.max(num1,num2,...) |
Returns the greatest input number. The following example returns 2. Math.max(-2,-1,0,1,2); |
Math.sin(num) |
Returns the sine of num, where num is given in radians. The following example returns 1. Math.sin(Math.PI / 2); |
Math.cos(num) |
Returns the cosine of num, where num is given in radians. The following example returns -1. Math.cos(Math.PI); |
Math.tan(num) |
Returns the tangent of num, where num is given in radians. The following example returns 1.5574077246549023. Math.tan(1); |
Math.asin(num) |
Returns the arcsine in radians of num. The following example returns TRUE. Math.asin(1) == Math.PI / 2; |
Math.acos(num) |
Returns the arccosine of num. The following example returns TRUE. Math.acos(-1) == Math.PI; |
Math.atan(num) |
Returns the arctangent of num. The following example returns 1. Math.atan(1.5574077246549023); |
Math.atan2(y, x) |
Returns the arctangent of the quotient of y divided by x. The following example returns 1.4056476493802699. Math.atan2(90,15); |
Math.ceil(num) |
Rounds up num to the nearest integer. The following example returns 2. Math.ceil(1.2); |
Math.floor(num) |
Rounds down num to the nearest integer. The following example returns 1. Math.floor(1.7); |
Math.round(num) |
Rounds num up or down to the nearest integer. The following example returns 2. Math.round(1.7); |
Math.exp(num) |
Returns the result of raising the mathematical constant e to the value of num. The following example returns TRUE. Math.exp(2) == Math.pow(Math.E,2); |
Math.log(num) |
Returns the natural (base-e) logarithm of num. The following example returns 2.302585092994046 . Math.log(10); |
Math.sqrt(num) |
Returns the square root of num. The following example returns 3. Math.sqrt(9); |
Math.pow(num, exponent) |
Returns the result of raising num to the value of exponent. The following example returns 8. Math.pow(2,3); |
Math.random() |
Returns a random number between zero and one. The following example returns a random number. Math.random(); |
Constant | Value |
---|---|
Math.PI | 3.141592653589793, the constant pi |
Math.E | 2.718281828459045, the constant e |
Math.LN2 | 0.6931471805599453, the natural (base-e) logarithm of two |
Math.LN10 | 2.302585092994046, the natural (base-e) logarithm of ten |
Math.LOG2E | 1.4426950408889634, the base-2 logarithm of the constant e |
Math.LOG10E | 0.4342944819032518, the base-10 logarithm of the constant e |
Math.SQRT2 | 1.4142135623730951, the square root of two |
Math.SQRT1_2 | 0.7071067811865476, the square root of .5 |
The following example gets a random value within a specified range:
function getRandom(min, max) { return Math.random() * (max - min) + min; } getRandom(50,75);